如何在Pandas中删除包含特定字符串的行 | 您所在的位置:网站首页 › pandas 删除全0行 › 如何在Pandas中删除包含特定字符串的行 |
如何在Pandas中删除包含特定字符串的行
在这篇文章中,我们将看到如何在pandas中删除包含特定字符串的行。现在,为了删除含有特定字符串的行,我们可以使用pandas库中的contains()函数。 语法: series.str.contains(string, case=True, flags=0, na=None, regex=True) 返回值 – 布尔值的系列或索引 基本上,这个函数将在给定的列中搜索字符串,并返回相应的行。为此,我们需要通过使用这个函数过滤数据框来创建一个新的数据框。 语法: df[ df[ “column” ].str.contains( “someString” )==False ] 例子:创建数据框架 # Importing the library import pandas as pd # Dataframe df = pd.DataFrame({'team': ['Team 1', 'Team 1', 'Team 2', 'Team 3', 'Team 2', 'Team 3'], 'Subject': ['Math', 'Science', 'Science', 'Math', 'Science', 'Math'], 'points': [10, 8, 10, 6, 6, 5]}) # display df输出 : 在这个方法中,我们将用str.contains()函数来寻找行,该函数基本上会从系列中获取字符串,并检查是否与给定的字符串相匹配,使用布尔值来选择行,并将其设置为False将帮助我们忽略所选的行,并保留剩余的行。 语法: df[df[“column_name”].str.contains(“string”)==False] 示例 : 在下面的例子中,我们将选择除 “团队1 “以外的所有团队。 # importing the library import pandas as pd # Dataframe df = pd.DataFrame({'team': ['Team 1', 'Team 1', 'Team 2', 'Team 3', 'Team 2', 'Team 3'], 'Subject': ['Math', 'Science', 'Science', 'Math', 'Science', 'Math'], 'points': [10, 8, 10, 6, 6, 5]}) # Dropping the team 1 df = df[df["team"].str.contains("Team 1") == False] df输出 : 与方法1相同,我们在这里遵循同样的步骤,但用位或运算符来增加一个额外的字符串来搜索。 语法: df = df[df[“column_name”].str.contains(“string1|string2”)==False] 示例 : 在下面的程序中,我们将删除包含 “团队1 “或 “团队2 “的行。 # importing the library import pandas as pd # Dataframe df = pd.DataFrame({'team': ['Team 1', 'Team 1', 'Team 2', 'Team 3', 'Team 2', 'Team 3'], 'Subject': ['Math', 'Science', 'Science', 'Math', 'Science', 'Math'], 'points': [10, 8, 10, 6, 6, 5]}) # Dropping the rows of team 1 and team 2 df = df[df["team"].str.contains("Team 1|Team 2") == False] # display df输出 : 在这里,我们使用相同的函数,用一个连接方法,携带我们需要搜索的单词部分。 语法: df[ ~df.column_name.str.contains(‘|’.join([“string”])) ] 示例: 在下面这个程序中,情况与上述两种情况不同。在这里,我们将选择并删除带有给定部分字符串的行。例如,我们将删除在科目栏中带有 “Sci “的行。 # importing the library import pandas as pd # Dataframe df = pd.DataFrame({'team': ['Team 1', 'Team 1', 'Team 2', 'Team 3', 'Team 2', 'Team 3'], 'Subject': ['Math', 'Science', 'Science', 'Math', 'Science', 'Math'], 'points': [10, 8, 10, 6, 6, 5]}) # Dropping the rows with "Sci" # identify partial string discard = ["Sci"] # drop rows that contain the partial string "Sci" df[~df.Subject.str.contains('|'.join(discard))] #display df输出: |
CopyRight 2018-2019 实验室设备网 版权所有 |